package com.kuangstudy.controller.zset;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2021/6/7 14:53
 */
@RestController
@Log4j2
public class WeiboController {

    @Autowired
    private RedisTemplate redisTemplate;

    // 24小时
    @GetMapping("/bbs/hour")
    public Set hourdata() {
        // 1: 计算当前小时的key
        long hour = System.currentTimeMillis() / (1000 * 60 * 60);
        // 2: 从redis缓存中获取近24小时的数据
        Set set = this.redisTemplate.opsForZSet().reverseRangeWithScores("rank:hour:" + hour, 0, 20);
        return set;
    }

    // 一天
    @GetMapping("/bbs/day")
    public Set daydata() {
        Set set = this.redisTemplate.opsForZSet().reverseRangeWithScores("rank:day", 0, 20);
        return set;
    }

    // 周
    @GetMapping("/bbs/week")
    public Set weekdata() {
        Set set = this.redisTemplate.opsForZSet().reverseRangeWithScores("rank:week", 0, 20);
        return set;
    }

    // 月
    @GetMapping("/bbs/month")
    public Set monthdata() {
        Set set = this.redisTemplate.opsForZSet().reverseRangeWithScores("rank:month", 0, 20);
        return set;
    }
}
